package com.study.spmongodb.book.core1;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/15 20:33
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TaklingClock {
    private int interval;
    private boolean beep;

    public static void main(String[] args) {
        final LocalDate localDate1 = LocalDate.of(2020, 11, 1).withDayOfYear(2);
        System.out.println(localDate1);
        //计算 本周星期二
        final LocalDateTime with = LocalDateTime.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
        System.out.println(with);
        //计算某月第一个星期二
        final LocalDate localDate = LocalDate.of(2020, 11, 1).with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
        System.out.println(localDate);
    }

    public void start() {

        final Stream<String> works = Stream.empty();
        final Stream<String> stringStream = works.map(String::toLowerCase);
        works.sorted(Comparator.comparing(String::length).reversed());
        final Optional<String> startWithQ = works.filter(s -> s.startsWith("Q")).findFirst();
        //计算时间加减
        final LocalDate localDate = LocalDate.of(2020, 1, 1).plusDays(255);
        System.out.println(localDate);

        final LocalDate plus = localDate.plus(Period.ofYears(1));

        System.out.println(plus);
    }

    public static Stream<String> letters(String s) {
        final ArrayList<String> result = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            result.add(s.substring(i, i + 1));
        }
        //截取子流
        final Stream<Double> randoms = Stream.generate(Math::random).limit(100);


        return result.stream();
    }

    //TimePrinter 位于TaklingClock 内部，并不意味着每个 TaklingClock 都要有一个 TimePrinter 实例域
    //
    public class TimePrinter implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("At the tone ,the time is " + new Date());
            //内部类既可以 访问自身的数据域 ，也可以访问创建它的外围类对象的数据域
            if (beep) {
                System.out.println("beep");
            }
        }

    }


}
